<?php declare(strict_types=1);

namespace App\Api\V2\Controller\Store;


use App\Api\Controllers\BaseController;
use App\Models\Store;
use App\Models\StorePayWay;
use App\Models\User;
use App\Traits\ResponseTrait;
use App\Util\ListUtil;
use Illuminate\Http\Request;

class StorePayWayController extends BaseController
{

    use ResponseTrait;

    public function getPayAmountE(Request $request): string
    {
        $store_id = $request->post("store_id");
        $company = $request->post("company");
        $user = $this->parseToken();
        $systemUserArray = User::query()->select("id", "pid", "name")->get()->toArray();
        $systemUserArray = ListUtil::createListsNode($systemUserArray);
        $userCollection = ListUtil::getListsNode($systemUserArray, ["id" => $user->user_id]);
        $userCollection = ListUtil::listsNodeToArray([$userCollection]);
        $userCollectionIds = array_column($userCollection, "id");

        $store = Store::query()->whereIn("user_id", $userCollectionIds)->where("store_id", $store_id)->first();
        if (empty($store)) {
            return $this->errorResponse("店铺不存在");
        }
        //更新
        $storePayWay = StorePayWay::query()->select(["company", "pay_amount_e"])->where([
            "store_id" => $store_id,
            "company" => $company
        ])
            ->groupBy(["company", "pay_amount_e"])
            ->first();
        if (empty($storePayWay)) {
            return $this->errorResponse("通道不存在");
        }

        return $this->successResponse("ok", ["data" => $storePayWay->toArray()]);
    }

    //更新通道限额
    public function upPayAmountE(Request $request): string
    {
        $store_id = $request->post("store_id");
        $company = $request->post("company");
        $user = $this->parseToken();
        $pay_amount_e = $request->post("pay_amount_e");
        if ($pay_amount_e < 0) {
            return $this->errorResponse("限额不能小于0");
        }
//获取下级代理商
        $systemUserArray = User::query()->select("id", "pid", "name")->get()->toArray();
        $systemUserArray = ListUtil::createListsNode($systemUserArray);
        $userCollection = ListUtil::getListsNode($systemUserArray, ["id" => $user->user_id]);
        $userCollection = ListUtil::listsNodeToArray([$userCollection]);
        $userCollectionIds = array_column($userCollection, "id");

        $store = Store::query()->whereIn("user_id", $userCollectionIds)->where("store_id", $store_id)->first();
        if (empty($store)) {
            return $this->errorResponse("店铺不存在");
        }
        //更新
        $storePayWayCount = StorePayWay::query()->where([
            "store_id" => $store_id,
            "company" => $company
        ])->count();
        if ($storePayWayCount == 0) {
            return $this->errorResponse("店铺支付通道不存在");
        }
        StorePayWay::query()->where([
            "store_id" => $store_id,
            "company" => $company
        ])->update([
            "pay_amount_e" => $pay_amount_e
        ]);
        return $this->successResponse("ok", ["data" => true]);
    }


    public function getSettlementType(Request $request): string
    {
        $company = $request->post("company");
        $store_id = $request->post("store_id");
        $user = $this->parseToken();
        $systemUserArray = User::query()->select("id", "pid", "name")->get()->toArray();
        $systemUserArray = ListUtil::createListsNode($systemUserArray);
        $userCollection = ListUtil::getListsNode($systemUserArray, ["id" => $user->user_id]);
        $userCollection = ListUtil::listsNodeToArray([$userCollection]);
        $userCollectionIds = array_column($userCollection, "id");
        $store = Store::query()->whereIn("user_id", $userCollectionIds)->where("store_id", $store_id)->first();
        if (empty($store)) {
            return $this->errorResponse("店铺不存在");
        }
        //更新
        $pay=StorePayWay::query()->select([
            "settlement_type",
            "settlement_rate"
        ])->where([
            "store_id" => $store_id,
            "company" => $company,
        ])->first();
        if(empty($pay)){
            return $this->errorResponse("通道不存在");
        }
        return $this->successResponse("ok", ["data" => $pay->toArray()]);
    }

    public function upSettlementType(Request $request): string
    {
        $type = $request->post("settlement_type");
        $company = $request->post("company");
        $store_id = $request->post("store_id");
        $rate = $request->post("settlement_rate", 0.00);
        if (!in_array($type, config("settle.type"))) {
            return $this->errorResponse("结算方式错误");
        }
        if ($type != "D0" && $rate > 0) {
            return $this->errorResponse("只有D0可以设置结算费率");
        }
        if ($rate >= 0.1) {
            return $this->errorResponse("范围不到超过0.1");
        }

        if ($rate < 0) {
            return $this->errorResponse("范围必须大于0");
        }

        $user = $this->parseToken();
        $systemUserArray = User::query()->select("id", "pid", "name")->get()->toArray();
        $systemUserArray = ListUtil::createListsNode($systemUserArray);
        $userCollection = ListUtil::getListsNode($systemUserArray, ["id" => $user->user_id]);
        $userCollection = ListUtil::listsNodeToArray([$userCollection]);
        $userCollectionIds = array_column($userCollection, "id");
        $store = Store::query()->whereIn("user_id", $userCollectionIds)->where("store_id", $store_id)->first();
        if (empty($store)) {
            return $this->errorResponse("店铺不存在");
        }
        //更新
        $storePayWayCount = StorePayWay::query()->where([
            "store_id" => $store_id,
            "company" => $company,
            "status" => 1
        ])->count();
        if ($storePayWayCount > 0) {
            return $this->errorResponse("通道已经开通不允许修改");
        }
        StorePayWay::query()->where([
            "store_id" => $store_id,
            "company" => $company,
        ])
            ->where("status", "!=", 1)
            ->update([
                "settlement_type" => $type,
                "settlement_rate" => $rate
            ]);
        return $this->successResponse("ok", ["data" => true]);
    }

}